<template>
  <div>
    <h1>问题管理</h1>
    <el-row>
      <el-button type="danger" round @click="questions">新增问题</el-button>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        prop="faqContent"
        label="问题"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="faqAnswer"
        label="答案"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="lookNums"
        label="访问量"
        width="180"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="updateQuestion(scope.$index, scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-if="scope.row.delFlag === 0"
            size="mini"
            type="danger"
            @click="deleteQuestion(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :page-sizes="[1, 3, 5, 7]"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>

    <!-- 添加问题 -->
    <el-dialog title="添加问题" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="问题名称" :label-width="formLabelWidth">
          <el-input v-model="form.faqContent" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="问题答案" :label-width="formLabelWidth">
          <el-input v-model="form.faqAnswer" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addQuestion">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改问题 -->
    <el-dialog title="修改问题" :visible.sync="dialogFormVisible1">
      <el-form :model="updateForm">
        <el-form-item label="问题名称" :label-width="formLabelWidth">
          <el-input
            v-model="updateForm.faqContent"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="问题答案" :label-width="formLabelWidth">
          <el-input
            v-model="updateForm.faqAnswer"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="updateQuestions">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getFaqList } from '@/api/health'
  import { addFaq, updateQuestions, deleteFaq } from '@/api/customer'
  export default {
    name: 'Index',
    data() {
      return {
        pageNum: 1,
        pageSize: 4,
        total: 20,
        tableData: [
          {
            faqId: '',
            faqContent: '',
            faqAnswer: '',
            lookNums: '',
            createTime: '',
            updateTime: '',
            delFlag: '',
          },
        ],
        dialogFormVisible: false,
        dialogFormVisible1: false,
        form: {
          faqId: '',
          faqContent: '',
          faqAnswer: '',
          lookNums: '',
          createTime: '',
          updateTime: '',
          delFlag: '',
        },
        updateForm: {
          faqId: '',
          faqContent: '',
          faqAnswer: '',
          lookNums: '',
          createTime: '',
          updateTime: '',
          delFlag: '',
        },
        formLabelWidth: '120px',
      }
    },
    mounted() {
      console.log('mounted...')
      this.getFaqList1()
    },
    methods: {
      getFaqList1() {
        getFaqList(this.pageNum, this.pageSize).then((res) => {
          this.tableData = res.data
          this.total = res.total
          console.log(res)
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.getFaqList1()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.pageNum = val
        this.getFaqList1()
      },
      questions() {
        this.dialogFormVisible = true
      },
      addQuestion() {
        this.dialogFormVisible = false
        addFaq(this.form).then((res) => {
          this.dialogFormVisible = false
          this.getFaqList1()
          console.log(res)
        })
      },
      updateQuestion(index, row) {
        console.log(index, row)
        this.updateForm = row
        this.dialogFormVisible1 = true
      },
      updateQuestions() {
        updateQuestions(this.updateForm).then((res) => {
          this.dialogFormVisible1 = false
          // this.getFaqList1()
          this.$router.go(0)
          console.log(res)
        })
      },
      deleteQuestion(index, row) {
        console.log(index, row)
        deleteFaq(row).then((res) => {
          this.$router.go(0)
          console.log(res)
        })
      },
    },
  }
</script>

<style scoped></style>
